package org.dandelion.account.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class DBHelper {
    private static final String DATABASE_NAME = "toolbox";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "accounts";
    private static final String ID = "_id";
    private static final String TYPE = "type";
    private static final String MONEY = "money";
    private static final String RB = "rb";
    private static final String INFO = "info";
    private static final String DATE = "date";
    private DBOpenHelper helper;
    private SQLiteDatabase db;

    private static class DBOpenHelper extends SQLiteOpenHelper {
        private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + ID
                + " integer primary key autoincrement, " + TYPE + " text not null, " + MONEY
                + " text not null, " + RB + " text not null, " + INFO + " text not null, " +
                DATE + " text not null);";

        public DBOpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table if exists " + TABLE_NAME);
            onCreate(db);
        }
    }

    public DBHelper(Context context) {
        helper = new DBOpenHelper(context);
        db = helper.getWritableDatabase();
    }

    public void insert(Account account) {
        ContentValues values = new ContentValues();
        values.put(TYPE, account.getType());
        values.put(MONEY, account.getMoney());
        values.put(RB, account.getRb());
        values.put(INFO, account.getInfo());
        values.put(DATE, account.getDate());
        db.insert(TABLE_NAME, null, values);
    }

    public void delete(int id) {
        db.delete(TABLE_NAME, "_id = ?", new String[]{String.valueOf(id)});
    }

    public List<Account> query(String date) {
        Account account = null;
        List<Account> list = new ArrayList<>();
        Cursor cursor = db.query(TABLE_NAME, new String[]{ID, TYPE, MONEY, RB, INFO, DATE},
                "date = ?", new String[]{date}, null, null, null);
        while (cursor.moveToNext()) {
            account = new Account();
            account.setId(cursor.getInt(0));
            account.setType(cursor.getString(1));
            account.setMoney(cursor.getDouble(2));
            account.setRb(cursor.getString(3));
            account.setInfo(cursor.getString(4));
            account.setDate(cursor.getString(5));
            list.add(account);
        }
        cursor.close();
        return list;
    }

    public double getBalance(String date) {
        double balance = 0.0;
        Cursor cursor = db.query(TABLE_NAME, new String[]{ID, TYPE, MONEY, INFO},
                "date = ?", new String[]{date}, null, null, null);
        while (cursor.moveToNext()) {
            if (cursor.getString(1).equals("收入")) {
                balance = balance + cursor.getDouble(2);
            } else if (cursor.getString(1).equals("支出")) {
                balance = balance - cursor.getDouble(2);
            }
        }
        cursor.close();
        return balance;
    }
}
